<?
class Rule_UniqueDocWithParent extends Rule
{
    public $classid;
    public $parentid;
    public $error_message = "Значение уже существует";

    function __construct($name, $classid, $parentid, $error_message = "", $id = 0)
    {
        $this->name = $name;
        $this->classid = $classid;
        $this->parentid = $parentid;
        $this->id = $id;
        if ($error_message != "")
            $this->error_message = $error_message;
    }

    function Check($hash)
    {
        if ($this->id) {
            $count = DB::scalarSelect("select count(*) from document where parentid={$this->parentid} and classid={$this->classid} and title = '" .
                DB::escape($hash[$this->name]) . "' and id!=" . $this->id);
        } else {
            $count = DB::scalarSelect("select count(*) from document where parentid={$this->parentid} and classid={$this->classid} and title = '" .
                DB::escape($hash[$this->name]) . "'");
        }

        return ($count == 0 ? true : false);
    }
}
